/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.core.HashCodeFactory;
import com.bourre.data.collections.Map;

import fever.events.FrameEvent;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

/**
 * Allows listening and control of <strong>MovieClip</strong> playhead.
 *
 * @author Romain Ecarnot
 */
class fever.display.MCFrameController
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : MCFrameController;
	
	private var _listeners : Map;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link MCFrameController} instance.
	 */
	public static function getInstance(  ) : MCFrameController
	{
		if( !_instance ) _instance = new MCFrameController();
		return _instance;
	}
	
	/**
	 * Adds a script for passed-in {@code target} at frame {@code index}.
	 * 
	 * <p>Script insertion is unique per frame.
	 * 
	 * <p>Auto delegation is used ( Pixlib implementation )
	 */
	public function addFrameScript( target : MovieClip, index : Number, scope ) : Void
	{
		if( !_isValidIndex( target, index ) ) return;
		
		var f : Function = arguments[ 3 ];
		var key : Number = HashCodeFactory.getKey( target );
		var a : Map;
		
		if( !_listeners.containsKey( key ) )
		{
			a = new Map();
			_listeners.put( key, a );
		}
		else a = _listeners.get( key );
		
		if( !a.containsKey( index ) ) 
		{
			if ( f ) scope = _getEventProxy.apply( this, arguments.splice( 2 ) );
			a.put( index, scope );
		}
		else FeverDebug.WARN( 'Frame ' + index + ' is already under script control' );
		
		if( !target.__EB )
		{
			target.onEnterFrame = Delegate.create( this, _updateClipFrame, target );
			target.__EB = true;
		}
	}
	
	/**
	 * Removes script for passed-in {@code target} at frame {@code index}.
	 */
	public function removeFrameScript( target : MovieClip, index : Number ) : Void
	{
		var key : Number = HashCodeFactory.getKey( target );
		
		if( _listeners.containsKey( key ) )
		{
			var a : Map = _listeners.get( key );
			a.remove( index );
			
			if( a.isEmpty() ) 
			{
				_listeners.remove( key );
				
				if( _listeners.isEmpty() )
				{
					delete target.onEnterFrame;
					target.__EB = false;
				}	
			}
		} 
	}
	
	/**
	 * Shortcuts to add a simple {@code stop} command to passed-in {@code target} 
	 * at frame {@code index}.
	 * 
	 * <p>Caution : If a <code>stop();</code> script is inserted, you can't add another 
	 * one. Script insertion are unique per frame.
	 */
	public function addFrameStop( target : MovieClip, index : Number ) : Void
	{
		addFrameScript( target, index, this, _stop );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */ 
	private function MCFrameController() 
	{
		_listeners = new Map();
	}
	
	private function _isValidIndex( target : MovieClip, n : Number ) : Boolean
	{
		return ( target._totalframes >= n && 1 <= n);
	}
	
	/**
	 * Uses Pixlib EventBroadcaster#_getEventProxy implementation
	 */
	private function _getEventProxy( scope, method : Function ) : Function
	{
		return Delegate.create.apply( Delegate, [ scope, method ].concat( arguments.splice( 2 ) ) );
	}
	
	private function _updateClipFrame( target : MovieClip ) : Void
	{
		var index : Number = target._currentframe;
		
		if( index != target.__storeframe )
		{
			target.__storeframe = index;
			
			var key : Number = HashCodeFactory.getKey( target );
			
			if( _listeners.containsKey( key ) )
			{
				var a : Map = _listeners.get( key );
				if( a.containsKey( index ) )
				{
					_fireEvent( 
						a.get( index ), 
						new FrameEvent( FrameEvent.onFrameEVENT, target, index ) 
					);
				}
			}
		}
	}
	
	/**
	 * Uses Pixlib EventBroadcaster#_broadcast implementation
	 */
	private function _fireEvent( o, e : FrameEvent ) : Void
	{
		var s : String = typeof( o );
		
		if ( s == 'object' || s == 'movieclip' ) 
		{
			if ( o.handleEvent != undefined ) o.handleEvent( e );
			else o[ String( e.getType() ) ]( e );
		}
		else o.apply( this, [e] );
	}
	
	private function _stop( event : FrameEvent ) : Void
	{
		MovieClip( event.getTarget() ).stop();
	}
}